﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VideoClassLibrary.BusinessLogicLayer;
using System.Data;
using VideoClassLibrary.Model;

public partial class CourseListManagement_QuantityRegistCourseList : AdminUserBase
{

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!this.IsPostBack)
        {
            SetAutoCompleteContextKey();
        }

    }

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public static string[] GetTrainClassById(string prefixText, int count, string contextKey)
    {
        try
        {
            string[] result = null;
            TrainClassModule trainingModule = new TrainClassModule();
            DataSet ds = trainingModule.GetTrainclassListByWhere(int.Parse(contextKey), "  and LOWER(VTC_name) like '" + prefixText.ToLower() + "%'and IsPublish=1");
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                result = new string[ds.Tables[0].Rows.Count];
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    result[i] = "{'First':'" + dr["VTC_name"].ToString() + "','Second':" + dr["ID"].ToString() + "}";

                }
            }
            return result;
        }
        catch
        {
            // TODO: logging...
            return null;
        }
    }

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public static string[] GetClassRoomById(string prefixText, int count, string contextKey)
    {

        try
        {
            ClassRoomModule crm = new ClassRoomModule();
            string[] result = null;
            DataSet ds = crm.GetRoomListByWhereAndSID(" and Lower(VR_roomName) like '" + prefixText.ToLower() + "%' and IsPublished=1", int.Parse(contextKey));
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {

                result = new string[ds.Tables[0].Rows.Count];
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    result[i] = "{'First':'" + dr["VR_roomName"].ToString() + "','Second':" + dr["ID"].ToString() + "}";
                }
            }
            return result;

        }
        catch
        {

            return null;
        }
    }
    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public static string[] GetTeacherById(string prefixText, int count, string contextKey)
    {

        try
        {

            TeacherModule tm = new TeacherModule();
            string[] result = null;
            DataSet ds = tm.GetTeacherByNameAndId(prefixText, contextKey);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {

                result = new string[ds.Tables[0].Rows.Count];
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    result[i] = "{'First':'" + dr["VT_realname"].ToString() + "','Second':" + dr["ID"].ToString() + "}";
                }
            }
            return result;

        }
        catch
        {

            return null;
        }
    }
    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]

    public static string[] GetCourseById(string prefixText, int count, string contextKey)
    {
        try
        {
            CourseModule cm = new CourseModule();
            string[] info = contextKey.Split('|');
            string[] result = null;
            DataSet ds = cm.QueryCourseBySIDAndName(int.Parse(info[0]), int.Parse(info[1]), prefixText);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {

                result = new string[ds.Tables[0].Rows.Count];
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    result[i] = "{'First':'" + dr["VC_name"].ToString() + "','Second':" + dr["ID"].ToString() + "}";

                }
            }
            return result;


        }
        catch
        {

            return null;
        }
    }

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]

    public static string[] GetCourseTime(string prefixText, int count, string contextKey)
    {
        try
        {
            return GetCourseTimeList();

        }
        catch
        {

            return null;
        }
    }

    private void SetAutoCompleteContextKey()
    {

        for (int i = 1; i <= 10; i++)
        {
            HiddenField hidTrainingClassId = (HiddenField)this.FindControl("HidTrainClass" + i);

            ((AjaxControlToolkit.AutoCompleteExtender)this.FindControl("AceTrainClassName" + i)).ContextKey = this.SchoolID.ToString();
            ((AjaxControlToolkit.AutoCompleteExtender)this.FindControl("AcetxtClassRoom" + i)).ContextKey = this.SchoolID.ToString();
            ((AjaxControlToolkit.AutoCompleteExtender)this.FindControl("AcetxtTeacher" + i)).ContextKey = this.SchoolID.ToString();
            ((AjaxControlToolkit.AutoCompleteExtender)this.FindControl("AcetxtCourse" + i)).ContextKey = this.SchoolID.ToString() + "|" + hidTrainingClassId.Value;

        }
        
    }

    //数据添加录入
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        CourseListModule cm = new CourseListModule();
        int failCount = 0;
        int successCount = 0;
        for (int i = 1; i <= 10; i++)
        {
           int scID = this.CurrentUser.SchoolID;
           int hidTrainid = Convert.ToInt32(((HiddenField)this.FindControl("HidTrainClass" + i)).Value);
           int hidTeacherId = Convert.ToInt32(((HiddenField)this.FindControl("HidTeacher" + i)).Value);           
           int hidCourseId = Convert.ToInt32(((HiddenField)this.FindControl("HidCourse" + i)).Value);
           int hidRoomId = Convert.ToInt32(((HiddenField)this.FindControl("HidClassRoom" + i)).Value);
           TextBox txBeginDate=(TextBox)this.FindControl("txtStarDate"+i);
           TextBox txEndDate=(TextBox)this.FindControl("txtEndDate"+i);
           TextBox txStartTime=(TextBox)this.FindControl("txtStartime"+i);
           TextBox txEndTime=(TextBox)this.FindControl("txtEndTime"+i);


            Label labManage = (Label)this.FindControl("labManage" + i);

            if (hidTrainid != 0 /*&& hidTeacherId != 0*/ && hidRoomId != 0 && hidCourseId!=0
                && !string.IsNullOrEmpty(txBeginDate.Text.Trim()) && !string.IsNullOrEmpty(txEndDate.Text.Trim())
                 && !string.IsNullOrEmpty(txStartTime.Text.Trim()) && !string.IsNullOrEmpty(txEndTime.Text.Trim()))
            {
                V_courselist courseListModel = new V_courselist();
                GetCourseListModel(courseListModel, scID, hidTrainid, hidTeacherId, hidCourseId, hidRoomId, txBeginDate.Text,
                    txEndDate.Text, txStartTime.Text, txEndTime.Text);

                //Modiy by chenfeng 2011-03-16.
                //if (Showroom()==true)              
                //{
                    bool result = cm.AddCourseListByProcedure(courseListModel);
                    labManage.Text = result == true ? "成功" : "失败";
                    if (!result) failCount++;
                    else
                        successCount++;
                //}
            }
            else
            {
                labManage.Text = "信息不完整！";
                continue;
            }
            if (failCount > 0)
            {
 
            }
        }
        if (successCount > 0 && failCount == 0)
        {            
          PageHelper.AlterMsgAndRedirect("添加成功！","SearchCourseAllList.aspx");
        }
    }


    //检测教室，是否某段时间内有重复上课班级
    private bool Showroom()
    {
        CourseListModule cm = new CourseListModule();
        V_courselist co = new V_courselist();
        for (int i = 1; i <= 10; i++) 
        {
            Label labManage = (Label)this.FindControl("labManage" + i);
            TextBox txBeginDate = (TextBox)this.FindControl("txtStarDate" + i);
            TextBox txStartTime = (TextBox)this.FindControl("txtStartime" + i);
            TextBox txCourse = (TextBox)this.FindControl("txtCourse" + i);
            TextBox txRoom = (TextBox)this.FindControl("txtClassRoom" + i);
            string course = txCourse.Text;
            string roomname = txRoom.Text.ToString();
            DateTime starDate = Convert.ToDateTime(txBeginDate.Text);
            string startime = txStartTime.Text.ToString();
            DataSet ds =  cm.IfNameExist(roomname, starDate, startime, course);
            if (ds == null)
            {
                labManage.Text = "";
                return true;
            }
            else
            {
                labManage.Text = "请重新选择上课时间";
                return false;
            }
        
        }
        return true;


    }

    private void GetCourseListModel(V_courselist courseListModel, int scID, int hidTrainid, int hidTeacherId, int hidCourseId, int hidRoomId, string starDate, string endDate, string startTime, string endTime)
    {
        courseListModel.TrainClassId = Convert.ToInt32(hidTrainid);
        courseListModel.SchoolID = Convert.ToInt32(scID);
        courseListModel.RoomID = Convert.ToInt32(hidRoomId);
        courseListModel.teacherID = Convert.ToInt32(hidTeacherId);
        courseListModel.courseID = Convert.ToInt32(hidCourseId);
        courseListModel.VCL_startDate = Convert.ToDateTime(starDate);
        courseListModel.VCL_EndDate = Convert.ToDateTime(endDate);
        courseListModel.VCL_startTime = Convert.ToDateTime(startTime);
        courseListModel.VCL_EndTime = Convert.ToDateTime(endTime);

    }
    //事件回发
    protected void txtTrainClass_TextChanged(object sender, EventArgs e)
    {
        TextBox tbSender = (TextBox)sender;
        if (tbSender != null)
        {
            string txId = tbSender.ID;
            int indexNum = int.Parse(txId.Substring(13));
            HiddenField hidTrainingClassId = (HiddenField)this.FindControl("HidTrainClass" + indexNum);
            AjaxControlToolkit.AutoCompleteExtender AcetxtCourse = (AjaxControlToolkit.AutoCompleteExtender)this.FindControl("AcetxtCourse" + indexNum);
            AcetxtCourse.ContextKey = this.SchoolID.ToString() + "|" + hidTrainingClassId.Value;
        }

    }

    private static string[] GetCourseTimeList()
    {

        DateTime beginTime = new DateTime(2011, 1, 1, 9, 0, 0);
        DateTime endTime = new DateTime(2011, 1, 1, 22, 0, 0);
        List<string> timeList = new List<string>();
        while (endTime.Subtract(beginTime).Minutes >= 0)
        {
            beginTime = beginTime.AddMinutes(30);
            timeList.Add(beginTime.ToString("HH:mm"));
        }

        return timeList.ToArray();
    }
    protected void btnRedirectList_Click(object sender, EventArgs e)
    {
        Response.Redirect("SearchCourseAllList.aspx");
    }
    protected void btnCourseList_Click(object sender, EventArgs e)
    {
        Response.Redirect("SearchCourseAllList.aspx");
    }
}
